package com.example.Lezedata.ui.fragment.home;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.GoodsProjectModel;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.home.WinnersVc;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.example.Lezedata.util.GBInterface;
import com.example.Lezedata.util.GBUtil;

import java.util.ArrayList;
import java.util.List;

import bid.Bid;
import common.Common;


public class HomeWinners extends BaseFragment {

    public interface onTapHomeWinnersListener{
        void onTapHomeWinnersItem(View view , Bid.WinPrizeListResponse.WinPrizeItem model);
    }

    private RecyclerView gridView;
    private RecyclerView.Adapter adapter;
    public GBInterface viewCreatedListener;
    public onTapHomeWinnersListener onTapHomeWinnersListener;

    List<Bid.WinPrizeListResponse.WinPrizeItem> prizeItems;

    public static HomeWinners newInstance() {
        return new HomeWinners();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_winners_fragment, container, false);
    }

    @Override
    public void initView() {
        super.initView();
        if (viewCreatedListener != null){
            viewCreatedListener.onViewCreated(contentView);
        }
        findViewById(R.id.winner_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: Winner");
                baseVc().jumpActivity(WinnersVc.class);
            }
        });

        gridView = (RecyclerView)findViewById(R.id.winners_gridView);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(GridLayoutManager.HORIZONTAL);
        gridView.setLayoutManager(manager);
        setGridViewAdapter();
    }

    void setGridViewAdapter(){
        final View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTapHomeWinnersListener != null){
                    WinnersViewHolder holder = (WinnersViewHolder)gridView.findContainingViewHolder(v);
                    int position = holder.getAdapterPosition();
                    Bid.WinPrizeListResponse.WinPrizeItem model = prizeItems.get(position);
                    onTapHomeWinnersListener.onTapHomeWinnersItem(v,model);
                }
            }
        };
        adapter = new RecyclerView.Adapter<WinnersViewHolder>() {
            @NonNull
            @Override
            public WinnersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.winners_cell,parent , false);
                view.setOnClickListener(listener);
                return new WinnersViewHolder(view);
            }

            @Override
            public void onBindViewHolder(@NonNull WinnersViewHolder holder, int position) {
                holder.setData(prizeItems.get(position));
            }

            @Override
            public int getItemCount() {
                if (prizeItems == null) return 0;
                return prizeItems.size();
            }
        };

        gridView.setAdapter(adapter);
    }

    class WinnersViewHolder extends RecyclerView.ViewHolder{
        public WinnersViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public void setData(Bid.WinPrizeListResponse.WinPrizeItem model){
            ImageView imageView = itemView.findViewById(R.id.winner_goodsIcon);
            TextView textView_mobile = itemView.findViewById(R.id.winner_mobile);
            TextView textView_price = itemView.findViewById(R.id.winner_price);

            GBUtil.setGoodsImage(imageView,model.getImgUrl());
            String mobile = model.getPhone();
            mobile = GBUtil.mobileReplaceToXxx(mobile);
            textView_mobile.setText(mobile);

            String price = String.valueOf(model.getBidCount());
            String pStr = price + "bids";

            SpannableString showText = GBUtil.textAddColor(pStr,getResources().getColor(R.color.app_color_yellow),0,price.length());
            textView_price.setText(showText);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void reloadData(Bid.WinPrizeListResponse value){
        prizeItems = value.getListList();
        adapter.notifyDataSetChanged();
    }
}